<template>
  <div class="app-container">
     <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
        <el-form-item label="应用平台" prop="type">
          <el-select v-model="queryParams.type" placeholder="应用平台" clearable style="width: 200px">
               <el-option
                  v-for="dict in typeObject"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
               />
            </el-select>
        </el-form-item>
        <el-form-item label="版本状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="版本状态" clearable style="width: 200px">
               <el-option
                  v-for="dict in stateObject"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
               />
            </el-select>
        </el-form-item>

        <el-form-item>
           <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
           <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
     </el-form>

     <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
           <el-button
              type="primary"
              plain
              icon="Plus"
              @click="handleAdd"
              v-hasPermi="['system:versionManage:add']"
           >新增</el-button>
        </el-col>

     </el-row>

     <el-table v-loading="loading" :data="noticeList" >

        <el-table-column label="序号" align="center" prop="id" width="100" />
        <el-table-column
           label="版本号"
           align="center"
           prop="versionCode"
           :show-overflow-tooltip="true"
        />
        <el-table-column
           label="版本名称"
           align="center"
           prop="versionName"
           :show-overflow-tooltip="true"
        />

        <el-table-column label="版本信息" align="center" prop="versionInfo" >
           <template #default="scope">
             {{ scope.row.versionInfo ||'-' }}
           </template>
        </el-table-column>
        <el-table-column
           label="应用平台"
           align="center"
           prop="typeText"
           :show-overflow-tooltip="true"
        />
        <el-table-column
           label="更新类型"
           align="center"
           prop="updateTypeNameText"
           :show-overflow-tooltip="true"
        />
        <el-table-column label="状态" align="center" prop="status" width="100">
           <template #default="scope">
             {{ scope.row.status ==0?'未发布':'已发布' }}
           </template>
        </el-table-column>

        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
           <template #default="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
           </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
           <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:versionManage:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:versionManage:remove']" >删除</el-button>
           </template>
        </el-table-column>
     </el-table>

     <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
     />

     <!-- 添加或修改视频对话框 -->
     <el-dialog :title="title" v-model="open" width="780px" append-to-body>
        <el-form ref="appVersionRef" :model="form" :rules="rules" label-width="80px">
           <el-row>
              <el-col :span="12">
                 <el-form-item label="版本号" prop="versionCode">
                    <el-input-number  controls-position="right"  type="number" style="width: 200px" :min="100" v-model="form.versionCode" placeholder="请输入版本号" ></el-input-number>
                 </el-form-item>
              </el-col>
              <el-col :span="12">
                 <el-form-item label="版本名称" prop="versionName">
                    <el-input v-model="form.versionName" placeholder="请输入版本名称" />
                 </el-form-item>
              </el-col>

              <el-col :span="12">
              <el-form-item label="应用平台" prop="type">
                <el-select v-model="form.type" placeholder="应用平台"  style="width: 200px">
                    <el-option
                        v-for="dict in typeObject"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                  </el-select>
              </el-form-item>
              </el-col>
              <el-col :span="12">
              <el-form-item label="更新类型" prop="updateType">
                <el-select v-model="form.updateType" placeholder="更新类型"  style="width: 200px">
                    <el-option
                        v-for="dict in updateObject"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                  </el-select>
              </el-form-item>
            </el-col>
              <el-col :span="24">
                 <el-form-item label="状态">
                    <el-radio-group v-model="form.status">
                       <el-radio
                          v-for="dict in stateObject"
                          :key="dict.value"
                          :label="dict.value"
                       >{{ dict.label }}</el-radio>
                    </el-radio-group>
                 </el-form-item>
              </el-col>
              <el-col :span="24">
                 <el-form-item label="下载地址" prop="downloadUrl">
                    <el-input type="textarea" v-model="form.downloadUrl" placeholder="请输入下载地址" />
                 </el-form-item>
              </el-col>
              <el-col :span="24">
               <el-form-item label="文件" >
                 <FileUpload ref="FileUpload" :limit="1" v-model:modelValue="form.downloadUrl" ></FileUpload>
               </el-form-item>
             </el-col>
             <el-col :span="24">
                 <el-form-item label="版本信息" prop="versionInfo">
                    <el-input type="textarea" v-model="form.versionInfo" placeholder="请输入版本信息" />
                 </el-form-item>
              </el-col>
           </el-row>
        </el-form>
        <template #footer>
           <div class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
           </div>
        </template>
     </el-dialog>
  </div>
</template>

<script setup name="versionManage">
import { getAppVersionList, getAppVersion, delAppVersion, addAppVersion, updateAppVersion } from "@/api/versionManage/index";

const { proxy } = getCurrentInstance();



const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const typeObject = ref([
  {
    label:'android',
    value:1
  },
  {
    label:'ios',
    value:2
  }
]);
const stateObject = ref([
  {
    label:'未发布',
    value:0
  },
  {
    label:'已发布',
    value:1
  }
]);
const updateObject = ref([
  {
    label:'强制更新',
    value:1
  },
  {
    label:'弹窗确认更新',
    value:2
  },
  {
    label:'静默更新',
    value:3
  }
]);
const data = reactive({
 form: {},
 queryParams: {
   pageNum: 1,
   pageSize: 10,
   type:undefined,
   status: undefined
 },
 rules: {
  versionCode: [{ required: true, message: "版本号不能为空", trigger: "blur" }],
  versionName: [{ required: true, message: "版本名称不能为空", trigger: "blur" }],
  type: [{ required: true, message: "应用平台不能为空", trigger: "change" }],
  updateType: [{ required: true, message: "更新类型不能为空", trigger:"change" }],
  downloadUrl:[{ required: true, message: "下载地址不能为空", trigger: ["change","blur"] }],
 },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询视频列表 */
function getList() {
 loading.value = true;
 getAppVersionList(queryParams.value).then(response => {
  // debugger
   noticeList.value = response.rows;
   total.value = response.total;
   loading.value = false;
 });
}
/** 取消按钮 */
function cancel() {
 open.value = false;
 reset();
}
/** 表单重置 */
function reset() {
 form.value = {
   id: undefined,
   versionCode: undefined,
   type: 1,
   versionName: undefined,
   versionInfo:undefined,
   downloadUrl:undefined,
   updateType:undefined,
   status: 0,

 };
 proxy.resetForm("appVersionRef");
}
/** 搜索按钮操作 */
function handleQuery() {
 queryParams.value.pageNum = 1;
 getList();
}
/** 重置按钮操作 */
function resetQuery() {
 proxy.resetForm("queryRef");
 handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
 ids.value = selection.map(item => item.noticeId);
 single.value = selection.length != 1;
 multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
 reset();
 open.value = true;
 title.value = "添加版本";
}
/**修改按钮操作 */
function handleUpdate(row) {
 reset();
 const id = row.id || ids.value;
 getAppVersion(id).then(response => {
   response.data.status = response.data.status
   form.value = response.data;
   open.value = true;
   title.value = "修改版本";
 });
}
/** 提交按钮 */
function submitForm() {
 proxy.$refs["appVersionRef"].validate(valid => {
   if (valid) {

     if (form.value.id != undefined) {
       updateAppVersion(form.value).then(response => {
         proxy.$modal.msgSuccess("修改成功");
         open.value = false;
         getList();
       });
     } else {
      addAppVersion(form.value).then(response => {
         proxy.$modal.msgSuccess("新增成功");
         open.value = false;
         getList();
       });
     }
   }
 });
}
/** 删除按钮操作 */
function handleDelete(row) {
 const id = row.id
 proxy.$modal.confirm('是否确认删除版本号为"' + row.versionCode + '"的数据项？').then(function() {
   return delAppVersion(id);
 }).then(() => {
   getList();
   proxy.$modal.msgSuccess("删除成功");
 }).catch(() => {});
}

getList();
</script>
